/*
 * Copyright (c) 2009 Nokia Corporation
 */

#include "qxsrexample.h"

#include <QDomDocument>
#include <QDomElement>
#include <QDomNodeList>

QXSRExample::QXSRExample(QWidget *parent) : QMainWindow(parent) {
    setupUI();
}

QXSRExample::~QXSRExample() {

}

void QXSRExample::setupUI() {
    /* UI container */
    QFrame* frame = new QFrame(this);
    /* Layout we'll put our widgets. */
    _layout = new QVBoxLayout;
    frame->setLayout(_layout);
    _layout->addWidget(new QLabel("<h1>QXSR Example</h1>"),
                       0,
                       Qt::AlignCenter);

    QPushButton* parseXML = new QPushButton("Parse XML from example.xml");
    _layout->addWidget(parseXML);
    /* We'll parse the XML when the button is clicked. */
    connect(parseXML, SIGNAL(clicked()),this, SLOT(parseXML()));

    /* Let's make the UI scale so that we can scroll it. */
    QScrollArea* scrollArea = new QScrollArea;
    scrollArea->setWidget(frame);
    scrollArea->setWidgetResizable(true);

    setCentralWidget(scrollArea);
}

void QXSRExample::parseXML() {
    /* We'll parse the example.xml */
    QFile* file = new QFile(":/xml/example.xml");
    /* If we can't open it, let's show an error message. */
    if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
        QMessageBox::critical(this,
                              "QXSRExample::parseXML",
                              "Couldn't open example.xml",
                              QMessageBox::Ok);
        return;
    }
    
    QList< QMap<QString,QString> > persons;
    
    /* QDomDocument takes any QIODevice. as well as QString buffer*/
    QDomDocument doc("mydocument"); 
    //QString a="ssss";
    //doc.setContent(a,false);
    if (!doc.setContent(file)) 
    {          
    	return;      
	}      
    
    //Get the root element
    QDomElement docElem = doc.documentElement(); 
    
    // you could check the root tag name here if it matters
	QString rootTag = docElem.tagName(); // == persons
         
	// get the node's interested in, this time only caring about person's
	QDomNodeList nodeList = docElem.elementsByTagName("person");
    
	//Check each node one by one.
	for(int ii = 0;ii < nodeList.count(); ii++)
    {
		QMap<QString, QString> person;
		
		// get the current one as QDomElement
    	QDomElement el = nodeList.at(ii).toElement();

    	person["id"] = el.attribute("id"); // get and set the attribute ID

    	//get all data for the element, by looping throug all child elements
		QDomNode pEntries = el.firstChild();
        while(!pEntries.isNull()) {
        	QDomElement peData = pEntries.toElement();
        	QString tagNam = peData.tagName();
			
        	if(tagNam == "firstname") { /* We've found first name. */       			
				person["firstname"] = peData.text();
        	}else if(tagNam == "surname") { /* We've found surname. */
				person["surname"] = peData.text();
        	}else if(tagNam == "email") { /* We've found email. */
				person["email"] = peData.text();
        	}else if(tagNam == "website") { /* We've found website. */
				person["website"] = peData.text();
        	}

        	pEntries = pEntries.nextSibling();
        }
        
        persons.append(person);
     }
    
    this->addPersonsToUI(persons);
}


void QXSRExample::addPersonsToUI(QList< QMap<QString,QString> >& persons) {
    while(!persons.isEmpty()) {
        QGroupBox* personGB = new QGroupBox("Person");
        QFormLayout* layout = new QFormLayout;
        QMap<QString,QString> person = persons.takeFirst();
        layout->addRow("ID", new QLineEdit(person["id"]));
        layout->addRow("First name", new QLineEdit(person["firstname"]));
        layout->addRow("Surname", new QLineEdit(person["surname"]));
        layout->addRow("E-mail", new QLineEdit(person["email"]));
        layout->addRow("Website", new QLineEdit(person["website"]));
        personGB->setLayout(layout);
        this->_layout->addWidget(personGB);
    }
}
